let we = require('../../we/index.js')
new class extends we.Page {
    data() {
        return {
            status: '',
            hidden: true,
            shareImgUrl: ''
        }
    }

    onLoad(options) {
        this.setData({
            'status': options.status
        })
        wx.showLoading({
            title: '分享图生成中...',
        })
        let pic = options.goodsImg
        this.$getUserInfo().then(info=> {
            console.info("info.userInfo.avatarUrl", info.userInfo.avatarUrl)

            this.getImage(info.userInfo.avatarUrl, pic)
        }).catch(err=> {
            wx.hideLoading()
            wx.showToast({
                title: '绘制失败',
                icon: 'none'
            })
        })


    }

    getImage(avatar, pic) {
        let logo = '/assets/logo.png'
        let promise1 = new Promise(function (resolve, reject) {
            wx.getImageInfo({
                src: avatar,
                success: function (res) {
                    resolve(res);
                }
            })
        })
        let promise2 = new Promise(function (resolve, reject) {
            wx.getImageInfo({
                src: pic,
                success: function (res) {
                    resolve(res);
                }
            })
        })
        let promise3 = new Promise(function (resolve, reject) {
            wx.getImageInfo({
                src: logo,
                success: function (res) {
                    resolve(res);
                }
            })
        })
        Promise.all([promise1, promise2, promise3]).then(res=> {
            this.createImg(res)
        }).catch(err=> {
            wx.hideLoading()
            console.info("Promise.all", err)
            wx.showToast({
                title: err.errMsg || '绘制失败',
                icon: 'none'
            })
            setTimeout(()=> {
                if (this.data.status === 'prize') {
                    wx.redirectTo({
                        url: '/pages/goods/index'
                    })
                } else {
                    wx.navigateBack({delta: 0})
                }
            }, 500)

        })
    }

    createImg(res) {

        let that = this
        const ctx = wx.createCanvasContext('shareImg')

        ctx.setFillStyle('#FF473D')
        ctx.fillRect(0, 0, 359, 548)

        ctx.drawImage(res[0].path, 143, 8, 72, 72)
        ctx.drawImage(res[1].path, 0, 168, 343, 192)
        ctx.save()
        ctx.rect(0, 358, 343, 190)
        ctx.setFillStyle('#fff')
        ctx.setStrokeStyle('#fff')
        ctx.fill()
        ctx.drawImage('/' + res[2].path, 122, 396, 96, 96)

        ctx.setTextAlign('center')                        //  位置
        ctx.setFillStyle('#ffffff')                       //  颜色
        ctx.setFontSize(13)                               //  字号
        ctx.fillText('我已打败朋友圈90%好友，免费抽中大奖，快来', 170, 110)
        ctx.fillText('拼手气和我组团抽奖，更多惊喜大奖免费拿!', 170, 130)
        ctx.save()
        ctx.setFillStyle('#151515')                       //  颜色
        ctx.setFontSize(15)                               //  字号
        ctx.fillText('长按识别小程序，参与抽奖', 170, 518)
        ctx.save()
        ctx.stroke()
        ctx.draw(true, function () {
            wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: 339,
                height: 548,
                destWidth: 2872,
                destHeight: 4384,
                quality: 1,
                canvasId: 'shareImg',
                success: function (res) {
                    console.log('生成图片地址', res)
                    wx.hideLoading()
                    that.setData({
                        shareImgUrl: res.tempFilePath,
                        hidden: false
                    })
                },
                fail: function (err) {
                    wx.hideLoading()
                    wx.showToast({
                        title: err.errMsg,
                        icon: 'none'
                    })
                    setTimeout(()=> {
                        if (that.data.status === 'prize') {
                            wx.redirectTo({
                                url: '/pages/goods/index'
                            })
                        } else {
                            wx.navigateBack({delta: 0})
                        }
                    }, 500)
                }
            })
        })
    }

    savePic() {
        var that = this
        wx.showLoading({
            title: '图片保存中...',
        })
        wx.saveImageToPhotosAlbum({
            filePath: that.data.shareImgUrl,
            success(res) {
                wx.hideLoading()
                wx.showModal({
                    content: '图片已保存到相册，赶紧晒一下吧~',
                    showCancel: false,
                    confirmText: '好的',
                    confirmColor: '#333',
                    success: function (res) {
                        if (res.confirm) {
                            that.$post('/user/shareMoment').then((res)=> {
                            }).catch(err=> {
                            })
                            if (that.data.status === 'prize') {
                                wx.redirectTo({
                                    url: '/pages/goods/index'
                                })
                            } else {
                                wx.navigateBack({delta: 0})
                            }
                            that.setData({
                                hidden: true
                            })
                        }
                    }
                })
            },
            fail(){
                wx.hideLoading()
                wx.showToast({
                    title: '保存失败',
                    icon: 'none'
                })
            }
        })
    }

    save() {
        let that = this
        wx.getSetting({
            success: (res)=>{
                if (!res.authSetting['scope.writePhotosAlbum']) {
                    wx.authorize({
                        scope: 'scope.writePhotosAlbum',
                        success:(res)=>{
                            that.savePic()
                        },
                        fail: (res)=>{
                            wx.openSetting({
                                success: (res)=>{
                                    if(res.authSetting['scope.writePhotosAlbum']){
                                        wx.showToast({
                                            title: "授权成功",
                                            icon: 'none'
                                        })
                                    }else{
                                        wx.showToast({
                                            title: "授权失败，请打开'保存到相册'",
                                            icon: 'none'
                                        })
                                    }
                                    
                                },
                                fail: (res)=>{
                                    wx.showToast({
                                        title: "授权失败，请重试",
                                        icon: 'none'
                                    })
                                }
                            })
                        }
                    })
                }else{
                    that.savePic()  
                }
            }
        })
    }
}